package com.hivekion.system.service.impl;

import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.SysVersion;
import com.hivekion.system.domain.vo.version.SysVersionModelVo;
import com.hivekion.system.domain.vo.version.SysVersionUpdateVo;
import com.hivekion.system.mapper.SysVersionMapper;
import com.hivekion.system.service.ISysVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 系统信息 业务层处理
 *
 * @author guoying
 */
@Service
public class SysVersionServiceImpl extends ServiceImpl<SysVersionMapper, SysVersion> implements ISysVersionService {

    @Override
    public boolean edit(SysVersionUpdateVo versionUpdateVo) {

        if (!StringUtils.isNotBlank(versionUpdateVo.getName())) {
            throw new BusinessException(500, "名称不能为空！");
        }
        if (!StringUtils.isNotBlank(versionUpdateVo.getVersion())) {
            throw new BusinessException(500, "版本不能为空！");
        }
        if (!StringUtils.isNotBlank(versionUpdateVo.getVersionCode())) {
            throw new BusinessException(500, "版本编号不能为空！");
        }
        if (!StringUtils.isNotBlank(versionUpdateVo.getManufacturer())) {
            throw new BusinessException(500, "制造商不能为空！");
        }
        SysVersion sysVersion = this.getOne(null);
        if (sysVersion == null){
            sysVersion = create();
        }
        BeanUtils.copyProperties(versionUpdateVo,sysVersion);
        return this.updateById(sysVersion);
    }

    @Override
    public SysVersionModelVo getInfo() {

        SysVersionModelVo sysVersionModelVo= new SysVersionModelVo();
        SysVersion sysVersion = this.getOne(null);
        if (sysVersion == null) {
            sysVersion = create();
        }
        BeanUtils.copyProperties(sysVersion,sysVersionModelVo);
        return sysVersionModelVo;
    }

    /**
     * 查旬空--创建系统信息
     * @return
     */
    public SysVersion create(){

        SysVersion sysVersion = new SysVersion();
        sysVersion.setName("Panoram");
        sysVersion.setVersion("1.0");
        sysVersion.setVersionCode("10");
        sysVersion.setManufacturer("智能楼宇");

        this.save(sysVersion);
        return sysVersion;
    }
}
